﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TerhelesEloszto.Common;
using TerhelesEloszto.Engine;

namespace Presentation
{
	public partial class TeacherTimeTable : System.Web.UI.Page
	{

		private ITimeTable timeTable;


		private static IEngine engine = Engine.Instance();

		protected void Page_Load( object sender, EventArgs e )
		{
			try
			{
				String visitorName = User.Identity.Name;
				if( !engine.IsTeacher( visitorName ) )
				{
					if( engine.IsAdmin( visitorName ) )
					{
						Response.Redirect( "~/Teacher/AdminProblem.aspx" );
					}
					else
					{
						Response.Redirect( "~/Problem.aspx" );
					}

				}
				else
				{
					AssignmentStatus status = engine.StatusOfAssignment;
					if( status != AssignmentStatus.ok )
					{
						Response.Redirect( "~/Teacher/NoTimeTable.aspx" );

					}
					else
					{
						ITeacher me = engine.GetTeacherProfile( Page.User.Identity.Name );
						timeTable = me.PersonalTimeTable;
						List<IInterval> paros = timeTable.TaskList.Where( iv => iv.Parity == false ).ToList();
						List<IInterval> paratlan = timeTable.TaskList.Where( iv => iv.Parity == true ).ToList();
						CreateTimeTable( Table1, paratlan );
						CreateTimeTable( Table2, paros );

						List<ITask> tList = engine.GetTasks( true );
						List<ITask> myTasks = new List<ITask>();
						foreach( ITask t in tList )
						{
							if( t.RelatedTeacher != null )
							{
								if( ( t.RelatedTeacher.Neptun == me.Neptun ) && ( !t.Type.HasInterval ) )
								{
									myTasks.Add( t );
								}
							}
						}

						foreach( ITask t in myTasks )
						{
							BulletedList1.Items.Add( t.Name + " - " + t.RelatedFact.Name + " - heti " + t.Hours + " óra." );
							Label1.Text = "";
						}
					}
				}
			}
			catch( EngineException ex )
			{
				Response.Redirect( "~/Teacher/Error.aspx" );
			}
		}

		private void CreateTimeTable( Table t, List<IInterval> intervals )
		{
			t.BorderStyle = BorderStyle.Solid;
			t.BorderWidth = 3;
			t.BorderColor = System.Drawing.Color.Black;			
			t.GridLines = GridLines.Both;
			t.CellPadding = 5;


			for( int i = 0; i < 17; i++ )
			{
				t.Rows.Add( new TableRow() );
			}

			for( int h = 0; h < 17; h++ )
			{
				for( int i = 0; i < 8; i++ )
				{
					TableCell c = new TableCell();
					t.Rows[h].Cells.Add( c );
				}
			}

			t.Rows[0].Cells[0].Text = "Óra";
			t.Rows[0].Cells[1].Text = "Hétfő";
			t.Rows[0].Cells[2].Text = "Kedd";
			t.Rows[0].Cells[3].Text = "Szerda";
			t.Rows[0].Cells[4].Text = "Csütörtök";
			t.Rows[0].Cells[5].Text = "Péntek";
			t.Rows[0].Cells[6].Text = "Szombat";
			t.Rows[0].Cells[7].Text = "Vasárnap";
			t.Rows[0].CssClass = "bold";
			t.Rows[0].HorizontalAlign = HorizontalAlign.Center;

			for( int h = 6; h < 22; h++ )
			{
				t.Rows[h - 5].Cells[0].Text = h + " - " + ( h + 1 );
				t.Rows[h - 5].Cells[0].CssClass = "bold";
				t.Rows[h - 5].Cells[0].HorizontalAlign = HorizontalAlign.Center;
			}

			foreach( IInterval iv in intervals )
			{
				int x = (int)iv.Day;
				DoRowSpan( t, x, iv.BeginHour - 5, iv.EndHour - iv.BeginHour );
				t.Rows[iv.BeginHour - 5].Cells[x].Text = iv.RealtedTask.Name + " ( " + iv.RealtedTask.RelatedFact.Name + " ) ";
			}

			foreach(TableRow r in t.Rows)
			{
				foreach( TableCell c in r.Cells )
				{
					c.BorderWidth = 1;
				}
			}
		}

		private void DoRowSpan( Table t, int x, int y, int size )
		{
			t.Rows[y].Cells[x].RowSpan = size;
			for( int i = 1; i < size; i++ )
			{
				t.Rows[y + i].Cells.RemoveAt( x );
			}
		}
	}
}